package ru.housewife.service.userstables;

import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.UsersTableForm;
import ru.housewife.model.UsersTable;

/**
 * TODO: add description
 *
 * @author Alexander Troshanin
 */
public class UpdateUsersTable implements HibernateAction<Void> {

    private UsersTableForm usersTableForm;

    public UpdateUsersTable(UsersTableForm usersTableForm) {
        this.usersTableForm = usersTableForm;
    }


    @Override
    public Void execute(Session session) {
        UsersTable usersTable = (UsersTable) session.get(UsersTable.class, usersTableForm.getId());
        if (usersTable == null) {
            return null;
        }
        usersTable.setFrom(usersTableForm.getFrom());
        usersTable.setTo(usersTableForm.getTo());
        usersTable.setType(usersTableForm.getType());
        usersTable.setName(usersTableForm.getName());
        usersTable.setPeriodLength(usersTableForm.getPeriodLength());
        usersTable.setPermanent(usersTableForm.isPermanent());
        session.save(usersTable);
        return null;
    }
}
